package tichu_project252;

import java.util.Collections;
import java.util.LinkedList;
import tichu_project252.CardClasses.*;

/**
 * This class implements the deck of the game. The game has a deck of cards. In
 * this class we create the deck and shuffle it.
 *
 * <p> <b> Invariants: </b> The names of the cards and the suits.</p>
 *
 * @author Alexandra Barka, ΑΜ: 2867, login : barka.
 * @author Euaggelia Petauraki AM: 2875 login: petavraki.
 * @version %I%, %G%
 */
public class Deck {

    private LinkedList<Card> deck;      // The deck of the cards
    private String[] suits = {"Jade", "Sword", "Pagoda", "Star"}; //The suits of the cards
    String[] cardnames = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //The string card names

    /**
     * Constructs a new deck of tichu cards.
     *
     * <p><b>Constructor: </b> Deck()</p>
     *
     * <p><b>Precondition: </b> </p> <p><b>Postcondition: </b> Constructs a new
     * tichu deck </p>
     *
     * @throws Exception if a mistake occures
     */
    public Deck() throws Exception {
        deck = new LinkedList<Card>();  // we initialize our list

        for (int i = 2; i <= 14; i++) {
            for (int suit = 0; suit < 4; suit++) {
                deck.add(new simpleCard(i, cardnames[i - 2], suits[suit])); // we add all cards in the deck
            }
        }

        deck.add(new Mahjong()); // finally, we add the special cards
        deck.add(new Drache());
        deck.add(new Phoenix());
        deck.add(new Hund());

        for (int i = 0; i < deck.size(); i++) {
            deck.get(i).LoadImage("/tichu_project252/Images/Tichu_Cards/");
        }
    }

    /**
     * Shuffles the deck.
     *
     * Randomly permutes the specified list using a default source of
     * randomness.
     *
     * @see <a
     * href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)">shuffle</a>
     */
    public void shuffle() {
        Collections.shuffle(deck);
    }

    /**
     * Returns the size of the deck.
     *
     * <p> <b> Accessor: </b> size() </p>
     *
     * <p><b> Precondition: </b> The deck isn't empty-- the size would be zero.
     * </p> <p> <b> Postcondition:</b> Returns the size of the deck. </p>
     *
     * @return The size of the deck
     */
    public int size() {
        return deck.size();
    }

    /**
     * Removes and returns the first element from the list.
     *
     * <p> <b> Transformer(mutator)):remove()</b></p> <p><b>PreCondition:</b>
     * The deck has elements</p> <p><b>Postcondtion:</b> Safely remove the first
     * element of the list and return it.</p>
     *
     * @return the first element from the list
     */
    public Card remove() {

        return deck.removeFirst();
    }

    /**
     * Prints the elements of the deck.
     *
     * <p><b>Accessor:</b> print()</p> <p><b> Precondition: </b> </p> <p> <b>
     * Postcondition:</b> Prints the elements of the deck. </p>
     *
     */
    public void print() {
        for (int i = 0; i < deck.size(); i++) {
            if (deck.get(i) instanceof simpleCard) {
                simpleCard temp = (simpleCard) deck.get(i);
                System.out.println(temp.getstringRepr() + " " + temp.getSuit());

            } else {
                System.out.println(deck.get(i).getstringRepr());
            }
        }
    }
}
